package it.rikesh.aip2011.server.service;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.slim3.datastore.Datastore;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;

import it.rikesh.aip2011.client.service.FaunaFloraService;
import it.rikesh.aip2011.server.meta.FaunaFloraMeta;
import it.rikesh.aip2011.server.meta.TrailMeta;
import it.rikesh.aip2011.shared.model.FaunaFlora;
import it.rikesh.aip2011.shared.model.Trail;
import it.rikesh.aip2011.shared.model.TrailFaunaFlora;

/**
 * @author  RN
 */
public class FaunaFloraServiceImpl implements FaunaFloraService {
    private static final Logger log = Logger.getLogger(FaunaFloraServiceImpl.class.getName());
    private FaunaFloraMeta h = new FaunaFloraMeta();
    public String getFaunaDetail(String id) {
        Key key=Datastore.createKey(FaunaFlora.class, Long.parseLong(id));
        FaunaFlora fauna=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        return fauna.getDetail().getValue();
    }

    public String getFaunaImg(String id) {
        Key key=Datastore.createKey(FaunaFlora.class, Long.parseLong(id));
        FaunaFlora fauna=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        return fauna.getImg();
    }

    public List<FaunaFlora> getFaunaList() {
        return Datastore.query(h).asList();
    }

    public String getFaunaName(String id) {
        Key key=Datastore.createKey(FaunaFlora.class, Long.parseLong(id));
        FaunaFlora fauna=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        return fauna.getName();
    }

    public String getFaunaType(String id) {
        Key key=Datastore.createKey(FaunaFlora.class, Long.parseLong(id));
        FaunaFlora fauna=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        return fauna.getType();
    }

    public String setFauna(String name, String detail, String img, String type,String trail) {
        FaunaFlora ff=new FaunaFlora();
        ff.setDetail(new Text(detail));
        ff.setImg(img);
        ff.setName(name);
        ff.setType(type);
        TrailMeta tm=TrailMeta.get();
        Trail tr=Datastore.query(tm).filter(tm.name.equal(trail)).asSingle();
        TrailFaunaFlora TrFF=new TrailFaunaFlora();
        TrFF.getTrailRef().setModel(tr);
        TrFF.getFaunaFloraRef().setModel(ff);
        Datastore.put(ff);
        Datastore.put(TrFF);
        return "seccesso";
    }
    
    public List<Trail> getTrailFauna(String id){
        Key key=Datastore.createKey(FaunaFlora.class, Long.parseLong(id));
        FaunaFlora ffauna=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        List<Trail> list=new ArrayList<Trail>();
        for (TrailFaunaFlora ha : ffauna.getTrailFaunaFloraListRef().getModelList()) {
            list.add(ha.getTrailRef().getModel());
        }
        return list;
    }
    
    public String getFaunaId(String name){
        FaunaFlora fauna=Datastore.query(FaunaFloraMeta.get()).filter(FaunaFloraMeta.get().name.equal(name)).asSingle();
        log.info("Query getFaunaId("+name+") eseguito");
        return String.valueOf(fauna.getKey().getId());
        
    }
    
    public List<FaunaFlora> getFaunaByType(String type){
        return Datastore.query(FaunaFloraMeta.get())
            .filter(FaunaFloraMeta.get().type.equal(type))
            .asList();
    }

}
